﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UNSWThink
{
   public partial class ViewResponsesTabForm : Form
   {
      private DataTable relatedQuestions;
      private DataTable relatedResponses;

      private ArrayList questionResponses;
      private int offset;
      

      public ViewResponsesTabForm(DataTable relatedQuestions, DataTable relatedResponses)
      {
         InitializeComponent();

         this.relatedQuestions = relatedQuestions;
         this.relatedResponses = relatedResponses;

         this.questionResponses = new ArrayList();
         this.offset = 0;
      }

      private void ViewResponsesTabForm_Load(object sender, EventArgs e)
      {
         if (HelperMethods.CheckColumnsExists(relatedQuestions, Defs.QUESTION_COLUMN_NAMES))
         {
            for (int i = 0; i < relatedQuestions.Rows.Count; i++)
            {
               int fakeQuestionNum = i + 1;

               DataGridView tempGridView = new DataGridView();
               tempGridView.Dock = System.Windows.Forms.DockStyle.Fill;
               tempGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
               tempGridView.Name = CreateDataGridControlName(fakeQuestionNum);
               tempGridView.RowHeadersVisible = false;
               tempGridView.Size = new System.Drawing.Size(278, 400);
               tempGridView.TabIndex = fakeQuestionNum;
               tempGridView.AllowUserToAddRows = false;
               tempGridView.AllowUserToDeleteRows = false;
               tempGridView.ReadOnly = true;

               TabPage tempTabPage = new TabPage();
               tempTabPage.Controls.Add(tempGridView);
               tempTabPage.Name = CreateTabControlName(fakeQuestionNum);
               tempTabPage.TabIndex = fakeQuestionNum;
               tempTabPage.Text = "Question " + fakeQuestionNum;

               tabControlQuestions.TabPages.Add(tempTabPage);

               int questionID = (int)relatedQuestions.Rows[i]["id"];
               int realQuestionNum = (int)relatedQuestions.Rows[i]["number"];
               QuestionResponse newQuestionResponse = new QuestionResponse(questionID, realQuestionNum);
               newQuestionResponse.Responses.Columns.Add("description");
               questionResponses.Add(newQuestionResponse);
            }

            if (HelperMethods.CheckColumnsExists(relatedResponses, Defs.RESPONSE_COLUMN_NAMES))
            {
               // loop through responses table to change the totals
               for (int i = 0; i < relatedResponses.Rows.Count; i++)
               {
                  AddToQuestionResponse((int) relatedResponses.Rows[i]["question"],
                     relatedResponses.Rows[i]["description"].ToString());
               }
            }

            offset = relatedResponses.Rows.Count;

            InitDataGrid();
         }
      }

      private string CreateDataGridControlName(int questionNum)
      {
         return ("dataGridView" + questionNum.ToString());
      }

      private string CreateTabControlName(int questionNum)
      {
         return ("tabPage" + questionNum.ToString());
      }

      private int GetNumberFromTabName(string tabPageName)
      {
         string stringNum = tabPageName.Substring(7);
         return Int32.Parse(stringNum);
      }

      private void AddToQuestionResponse(int questionID, string description)
      {
         foreach (QuestionResponse quesResponse in questionResponses)
         {
            if (quesResponse.QuestionID == questionID)
            {
               object[] values = { description };
               quesResponse.Responses.Rows.Add(values);
               return;
            }
         }
      }

      private void InitDataGrid()
      {
         foreach (TabPage viewPage in tabControlQuestions.TabPages)
         {
            int tabNumber = GetNumberFromTabName(viewPage.Name);

            foreach (QuestionResponse quesResponse in questionResponses)
            {
               if (quesResponse.QuestionNum == tabNumber)
               {
                  string viewName = CreateDataGridControlName(tabNumber);
                  DataGridView dgv = (DataGridView)viewPage.Controls[viewName];
                  dgv.DataSource = quesResponse.Responses;

                  dgv.Columns["description"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

                  if (dgv.Columns["description"].Width < dgv.Width)
                  {
                     dgv.Columns["description"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                  }
                  else
                  {
                     dgv.Columns["description"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                  }

                  break;
               }
            }
         }
      }

      public void UpdateList()
      {
         if (offset < relatedResponses.Rows.Count)
         {
            if (HelperMethods.CheckColumnsExists(relatedResponses, Defs.RESPONSE_COLUMN_NAMES))
            {
               for (int i = offset; i < relatedResponses.Rows.Count; i++)
               {
                  AddToQuestionResponse((int)relatedResponses.Rows[i]["question"],
                     relatedResponses.Rows[i]["description"].ToString());
               }
            }
            offset = relatedResponses.Rows.Count;

            InitDataGrid();
         }
      }
   }
}
